package com.example.houduan.controller;


import com.example.houduan.common.lang.Result;
import com.example.houduan.common.lang.common.GetToken;
import com.example.houduan.common.lang.common.Tags;
import com.example.houduan.entity.Blog;
import com.example.houduan.entity.Eva;
import com.example.houduan.entity.Tag;
import com.example.houduan.entity.User;
import com.example.houduan.service.BlogService;
import com.example.houduan.service.EvaService;
import com.example.houduan.service.TagService;
import com.example.houduan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author anonymous
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    EvaService evaService;
    @Autowired
    BlogService blogService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    UserService userService;
    @Autowired
    TagService tagService;
    @PostMapping("/addblog")
    public Result addblog(@RequestBody Blog blog,HttpServletRequest request){
        String token= request.getHeader("token");
        User user= (User) redisTemplate.opsForValue().get(token);
        if(user==null||!user.getUsername().equals(blog.getUsername())){
            return Result.fail(400,"未登录",null);
        }
        if(blog.getTitle().isEmpty() ||blog.getContent().isEmpty()||blog.getDescription().isEmpty()){
            return Result.fail(420,"内容不能为空",null);
        }
        blog.setCreated(LocalDateTime.now());
        if(blog.getStatus()==null){
            blog.setStatus("原创");
        }else{
            blog.setStatus("转载至："+blog.getStatus());
        }
        blogService.addblog(blog);
        return Result.succ(200,"发布成功",blog);
    }
    @PostMapping("/evaluate")
    public Result evaluate(@RequestBody Eva eva,HttpServletRequest request){
        String token=request.getHeader("token");
        User user= (User) redisTemplate.opsForValue().get(token);
        if(user==null){
            return Result.fail(400,"未登录",null);
        }else {
            if(eva.getUsername().equals(user.getUsername())){
                evaService.addEva(eva);
                return Result.succ(200,"成功评论",null);
            }else {
                return Result.fail(444,"登陆状态异常",null);
            }
        }
    }
    @PostMapping("/forwardBlog")
    public Result forwardBlog(@RequestBody Blog blog,HttpServletRequest request){
        String token= request.getHeader("token");
        User user= (User) redisTemplate.opsForValue().get(token);
        if(user==null){
            return Result.fail(400,"未登录",null);
        }
        blog.setStatus(blog.getUsername());
        blog.setUsername(user.getUsername());
       return addblog(blog,request);

    }
    @GetMapping("/getblogByusername/{username}")
    public Result getByusername(@Validated @NotBlank @PathVariable String username){
        List<Blog> a=blogService.getByusername(username);
        if(a.isEmpty()){
            return Result.fail(425,"不存在该作者的博客",null);
        }
        return Result.succ(200,"查找成功",a);
    }
    @GetMapping("/getBlogByTag/{tag}")
    public  Result getBlogByTag(@Validated @NotBlank @PathVariable  String tag){
      List<Long> id=  tagService.getBytag(tag);
      List<Blog> blog =new ArrayList<>();
      for (Long o:id) blog.add(blogService.getById(o));
      if(blog.isEmpty())return Result.fail(400,"不存在",null);
      return Result.succ(200,"查找成功",blog);
    }
    @PostMapping("/addTag")
    public Result addTag( @RequestBody Tags tags){
        List<String> a=tags.getTag();
        if(a.isEmpty()){
            return Result.fail(400,"不存在tag",null);
        }
        for(String i:a){
            Tag m = new Tag(tags.getId(), i);
            tagService.addTag(m);
        }

        return Result.succ(200,"成功添加",null);
    }
    @DeleteMapping("/deleva")
    public Result deleva(@RequestBody Eva eva,HttpServletRequest request){
        String token=request.getHeader("token");
        User user= (User) redisTemplate.opsForValue().get(token);
        if(eva.getUsername()==null||eva.getId()==null){
            Result.fail(415,"参数错误",null);
        }
        if(user==null||!user.getUsername().equals(eva.getUsername())){
            return Result.fail(420,"登录状态错误",null);
        }
        evaService.delByeva(eva);
        return Result.succ(200,"请求成功",null);
    }
    @DeleteMapping("/deltag")
    public Result deltag(@RequestBody Tag tag){
        if(tag.getTag()==null||tag.getId()==null){
            Result.fail(415,"参数错误",null);
        }
        tagService.removeByTag(tag);
        return Result.succ(200,"请求成功",null);
    }
    @DeleteMapping("/deleteblog")
    public Result delBlog(@NotNull @RequestBody GetToken getToken,HttpServletRequest request){
           String token= request.getHeader("token");
            User user= (User) redisTemplate.opsForValue().get(token);
            if(user!=null){
                if(!Objects.equals(user.getId(), getToken.getUser_id())) {
                    return Result.fail(420,"不存在该博客或不是本文作者",null);
                }else if(blogService.getById(getToken.getId())==null){
                    return Result.fail(420,"不存在该博客",null);
                }else {
                    tagService.removeById(getToken.getId());
                    evaService.delByid(getToken.getId());
                    blogService.removeById(getToken.getId());
                    return Result.succ(200,"删除成功",null);
                }
            }
            return Result.fail(400,"未登录",null);
    }

    @PutMapping("/chengeByid")
    public Result chengeBlog(@RequestBody  Blog blog,HttpServletRequest request){
        if(blog.getContent().isEmpty()||blog.getTitle().isEmpty()||blog.getDescription().isEmpty()||blog.getId()==null){
            return Result.fail(420,"必要条件为空",null);
        }
        if(!blog.getStatus().equals("原创")){
            return Result.fail(421,"你不是本文作者",null);
        }
        if(blogService.getById(blog.getId())==null)return Result.fail(400,"不存在该博客",null);
        String token= request.getHeader("token");
        User user= (User) redisTemplate.opsForValue().get(token);
        if(user!=null){
            if(user.getUsername().equals(blog.getUsername())){
                blog.setCreated(LocalDateTime.now());
                blogService.updateById(blog);
                return Result.succ(200,"修改成功",null);
            }else{
                return Result.fail(421,"你不是本文作者",null);
            }
        }

        return Result.fail(400,"未登录",null);
    }
    @GetMapping("/getblog")
    public Result getBlog(){
       List<Blog>a= blogService.getTenBlog();
        if(a.isEmpty())return Result.fail(400,"不存在",a);
        return Result.succ(200,"成功请求十个随机blog",a);
    }
    @GetMapping("/getblogByshunxu/{fp}")
    public Result  getBlogNyShunxu(@PathVariable int fp){
        if(blogService.count()<(fp-1)*10){
            return Result.fail(400,"不存在",null);
        }
        return Result.succ(200,"成功请求",blogService.getTenBlogByShunxu((fp-1)*10));
    }
}
